package com.gitee.wsl.prism4j.languages

import com.gitee.wsl.prism4j.Prism4j
import com.gitee.wsl.prism4j.Prism4j.Companion.grammar
import com.gitee.wsl.prism4j.Prism4j.Companion.pattern
import com.gitee.wsl.prism4j.Prism4j.Companion.token
import com.gitee.wsl.prism4j.Prism4j.Pattern

@Suppress("unused")
object Prism_python {
    fun create(prism4j: Prism4j): Prism4j.Grammar {
        return grammar(
            "python",
            token(
                "comment", pattern(
                    Pattern.compile("(^|[^\\\\])#.*"),
                    true
                )
            ),
            token(
                "triple-quoted-string", pattern(
                    Pattern.compile("(\"\"\"|''')[\\s\\S]+?\\1"),
                    false,
                    true,
                    "string"
                )
            ),
            token(
                "string", pattern(
                    Pattern.compile("(\"|')(?:\\\\.|(?!\\1)[^\\\\\\r\\n])*\\1"),
                    false,
                    true
                )
            ),
            token(
                "function", pattern(
                    Pattern.compile("((?:^|\\s)def[ \\t]+)[a-zA-Z_]\\w*(?=\\s*\\()"),
                    true
                )
            ),
            token(
                "class-name", pattern(
                    Pattern.compile("(\\bclass\\s+)\\w+", Pattern.CASE_INSENSITIVE),
                    true
                )
            ),
            token(
                "keyword",
                pattern(Pattern.compile("\\b(?:as|assert|async|await|break|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|nonlocal|pass|print|raise|return|try|while|with|yield)\\b"))
            ),
            token(
                "builtin",
                pattern(Pattern.compile("\\b(?:__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\\b"))
            ),
            token("boolean", pattern(Pattern.compile("\\b(?:True|False|None)\\b"))),
            token(
                "number", pattern(
                    Pattern.compile(
                        "(?:\\b(?=\\d)|\\B(?=\\.))(?:0[bo])?(?:(?:\\d|0x[\\da-f])[\\da-f]*\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?j?\\b",
                        Pattern.CASE_INSENSITIVE
                    )
                )
            ),
            token(
                "operator",
                pattern(Pattern.compile("[-+%=]=?|!=|\\*\\*?=?|\\/\\/?=?|<[<=>]?|>[=>]?|[&|^~]|\\b(?:or|and|not)\\b"))
            ),
            token("punctuation", pattern(Pattern.compile("[{}\\[\\];(),.:]")))
        )
    }
}
